package gt.edu.usac.viewcontroller.backing.publico;

import gt.edu.usac.model.am.AppModuleImpl;

import gt.edu.usac.model.vo.PreasignacionViewImpl;
import gt.edu.usac.model.vo.preasignacionImpl;

import gt.edu.usac.viewcontroller.utils.JSFUtils;

import java.util.Iterator;

import javax.faces.application.ViewHandler;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.component.UIViewRoot;
import javax.faces.component.html.HtmlForm;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

import javax.faces.event.ActionEvent;

import javax.faces.event.ValueChangeEvent;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCDataControl;

import oracle.adf.view.faces.component.core.input.CoreInputText;


import oracle.adf.view.faces.component.core.input.CoreSelectOneChoice;
import oracle.adf.view.faces.context.AdfFacesContext;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import oracle.jbo.ApplicationModule;
import oracle.jbo.AttributeList;
import oracle.jbo.Row;
import oracle.jbo.domain.DBSequence;
import oracle.jbo.server.AttributeListImpl;
import oracle.jbo.server.SequenceImpl;

public class Preasignacion {
    private HtmlForm form1;
    private HtmlForm form2;
    private int idPersona=10;
    private BindingContainer bindings;
    private UIComponent foliobox;
    private CoreSelectOneChoice selectOneChoice1;
    private UISelectItems selectItems1;

    public void setForm1(HtmlForm form1) {
        this.form1 = form1;
    }

    public HtmlForm getForm1() {
        return form1;
    }
        
    public void setsIdPersona(ActionEvent event){
       /* FacesContext ctx = FacesContext.getCurrentInstance();
        
        //Creando el binding container 
        ValueBinding vb = ctx.getApplication().createValueBinding("#{data}");
        BindingContext bc = (BindingContext)vb.getValue(ctx);
        
        DCDataControl dc  = bc.findDataControl("AppModuleDataControl");
        ApplicationModule am = (ApplicationModule)dc.getDataProvider();
        AppModuleImpl service    = (AppModuleImpl)am;
        preasignacionImpl preasignacion = service.getpreasignacion1();
        try {
            Row preasignacionRow = preasignacion.getCurrentRow();
            preasignacionRow.setAttribute("IdPersona","2");
        }
        catch (Exception e) {
            this.idPersona="";
        }*/
    }
    

    public void setForm2(HtmlForm form2) {
        this.form2 = form2;
    }

    public HtmlForm getForm2() {
        return form2;
    }

    public BindingContainer getBindings() {
        return this.bindings;
    }

    public void setBindings(BindingContainer bindings) {
        this.bindings = bindings;
    }

    public String guardar_action(ActionEvent ae) {
        FacesContext ctx = FacesContext.getCurrentInstance();
       //Creando el binding container 
       ValueBinding vb = ctx.getApplication().createValueBinding("#{data}");
       BindingContext bc = (BindingContext)vb.getValue(ctx);
       
       DCDataControl dc  = bc.findDataControl("AppModuleDataControl");
       ApplicationModule am = (ApplicationModule)dc.getDataProvider();
       AppModuleImpl service    = (AppModuleImpl)am;
       
       //Obteniendo el valor de la secuencia que le toca
       SequenceImpl secuencia = new SequenceImpl("CAL_PERSONA_PREASIGNACION_SQ",service.getDBTransaction());
       oracle.jbo.domain.Number secuenciaNumber = (oracle.jbo.domain.Number)secuencia.getSequenceNumber();;
       this.idPersona = secuenciaNumber.intValue();
       
       //Colocando el valor de la secuencia en el registro
       PreasignacionViewImpl preasignacion = service.getPreasignacionView1();
       Row preasignacionRow = preasignacion.getCurrentRow();
       preasignacionRow.setAttribute("IdPersona",this.idPersona);
       
        BindingContainer bindings = getBindings();
        OperationBinding operationBinding = 
            bindings.getOperationBinding("Commit");
        Object result = operationBinding.execute();
        if (!operationBinding.getErrors().isEmpty()) {
            return null;
        }
        return null;
    }

    public void setIdPersona(int idPersona) {
        this.idPersona = idPersona;
    }

    public int getIdPersona() {
        return idPersona;
    }


    public void setFoliobox(UIComponent foliobox) {
        this.foliobox = foliobox;
    }

    public UIComponent getFoliobox() {
        return foliobox;
    }


    // Get the Component based on given UIComponent ID and refresh the component

       private static void refreshComponent(String pComponentID) {
           UIComponent component = findComponentInRoot(pComponentID);
           refreshComponent(component);
       }


       // Get Faces Context, Get Root Component, Find the given Component From the root component

       private static UIComponent findComponentInRoot(String pComponentID) {
           UIComponent component = null;
           FacesContext facesContext = FacesContext.getCurrentInstance();
           if (facesContext != null) {
               UIComponent root = facesContext.getViewRoot();
               component = findComponent(root, pComponentID);
           }
           return component;
       }


       // Refresh the Component

       private static void refreshComponent(UIComponent component) {
           if (component != null) {
               AdfFacesContext.getCurrentInstance().addPartialTarget(component);
           }
       }

      // Get the specific  component from a root component tree.

       private static UIComponent findComponent(UIComponent root, String id) {
           if (id.equals(root.getId()))
               return root;

           UIComponent children = null;
           UIComponent result = null;
           Iterator childrens = root.getFacetsAndChildren();
           while (childrens.hasNext() && (result == null)) {
               children = (UIComponent)childrens.next();
               if (id.equals(children.getId())) {
                   result = children;
                   break;
               }
               result = findComponent(children, id);
               if (result != null) {
                   break;
               }
           }
           return result;
       }


    public void setSelectOneChoice1(CoreSelectOneChoice selectOneChoice1) {
        this.selectOneChoice1 = selectOneChoice1;
    }

    public CoreSelectOneChoice getSelectOneChoice1() {
        return selectOneChoice1;
    }

    public void setSelectItems1(UISelectItems selectItems1) {
        this.selectItems1 = selectItems1;
    }

    public UISelectItems getSelectItems1() {
        return selectItems1;
    }
}
